Imports DAL
Imports System
Imports MyClasses
Imports System.Configuration


Public Class frmExpiredItem

    Dim strItemType As String = ""
    Dim CPurInv As CPurchaseInvoice
    Dim ObjRetItems() As CReturnItem
    Dim objItem() As CExpItem
    Dim dtDItemDtl As DataTable
    Dim strItem As String = String.Empty

    Private Sub frmExpiredItem_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.MdiParent = Nothing
    End Sub
    Private Sub frmExpiredItem_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            If Not (ConfigurationManager.AppSettings("FormMode") = "R") Then
                If isUpdate() Then
                    Select Case BCommon.ShowMsgError(10081, "YNC")
                        Case Windows.Forms.DialogResult.Yes
                            If ValidateInfo() = True Then
                                If Me.rdnBreakage.Checked = True Then
                                    Call SaveData()
                                Else
                                    Call SaveNonMoveData()
                                End If
                                Me.cfgExpItems.Rows.RemoveRange(1, Me.cfgExpItems.Rows.Count - 1)
                            Else
                                e.Cancel = True
                            End If
                        Case Windows.Forms.DialogResult.Cancel
                            e.Cancel = True
                        Case Windows.Forms.DialogResult.No
                    End Select
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnSave_Click")
        End Try
    End Sub

    Private Sub frmExpiredItem_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.F1 Then
            'Rauhl_ITS@20073012 : Add following code display help
            e.SuppressKeyPress = True
            Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Breakage Non Moving")
        End If
    End Sub

    Private Sub frmExpiredItem_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Call BCommon.SetFormStandards(Me)
        Call Grid_Settings()
        Call DisplayItems()
        '**************************************
        'Setting the Tab to Grid First Item
        'Me.btnSave.Visible = True
        Me.btnSave.Location = New Point(542, 13)

        'Me.cfgExpItems.Focus()
        'cfgExpItems.Row = 1
        Me.cfgExpItems.Col = 3
        Me.cmDItems.Focus()
        '================== 
    End Sub

    Private Sub Grid_Settings()
        Try
            cfgExpItems.Clear(C1.Win.C1FlexGrid.ClearFlags.All)
            Call SetGridCaptions()
            Call SetColumnWidths()

            ''Common Settings
            With cfgExpItems
                ' Hidden Columns
                .Cols(0).Visible = False                   'STOCKID
                .Cols(1).Visible = False                   'ITEMID
                .Cols(2).Visible = False                   'VENDORID
                .Cols(10).Visible = False                  'Re Qty

                'EDITABLE/NON-EDITABLE COLUMNS
                .Cols(3).Editor = cmDItems
                .Cols(4).Editor = CmbBatchNo
                .Cols(3).AllowEditing = True               'ITEMNAME
                .Cols(4).AllowEditing = True              'BATCHNO
                .Cols(5).AllowEditing = False              'EXPDATE
                .Cols(6).AllowEditing = True              'Reason
                .Cols(7).AllowEditing = True              'Qty
                'Divya30/01/2008
                '.Cols(8).AllowEditing = True              'BILLRATE
                If rdnNewItem.Checked = True Then
                    .Cols(8).AllowEditing = False
                Else
                    .Cols(8).AllowEditing = True
                End If
                '>>END
                .Cols(9).AllowEditing = False              'Amount
                .Cols(10).AllowEditing = False             'Re Qty

                .Cols(3).TextAlign = C1.Win.C1FlexGrid.TextAlignEnum.LeftCenter
                .Cols(4).TextAlign = C1.Win.C1FlexGrid.TextAlignEnum.LeftCenter

                .Cols(3).DataType = System.Type.GetType("System.String")
                .Cols(4).DataType = System.Type.GetType("System.String")
                .Cols(5).DataType = System.Type.GetType("System.DateTime")
                .Cols(5).DataType = System.Type.GetType("System.String")
                .Cols(6).DataType = System.Type.GetType("System.String")
                .Cols(7).DataType = System.Type.GetType("System.Int32")
                .Cols(8).DataType = System.Type.GetType("System.Decimal")
                .Cols(10).DataType = System.Type.GetType("System.Numeric")
                .Cols(8).Format = "0.00"                    'BILLRATE
                .Cols(9).Format = "0.00"                    'Amount
                .Rows.Count = 17
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "Grid_Settings")
        End Try
    End Sub


    Private Sub SetGridCaptions()
        Try
            With cfgExpItems
                .Cols.Count = 11
                '.Rows.Count = 2
                '.Rows.Fixed = 1
                .Cols(0).Caption = "Stock Id"
                .Cols(1).Caption = "Item Id"
                .Cols(2).Caption = "Vendor Id"
                .Cols(3).Caption = "Item Name"
                .Cols(4).Caption = "Batch No"
                .Cols(5).Caption = "Exp"
                .Cols(6).Caption = "Reason"
                .Cols(7).Caption = "Quantity"
                .Cols(8).Caption = "Bill Rate"
                .Cols(9).Caption = "Amount"
                .Cols(10).Caption = "Return Qty"
            End With
        Catch ex As Exception
        End Try
    End Sub


    Private Sub SetColumnWidths()
        Try
            With cfgExpItems
                'STOCKID,TS.ITEMID,TS.VENDORID,TS.BATCHNO,TS.EXPDATE,MP.ITEMNAME,TB.QTY,TS.BILLRATE
                .Cols(0).Visible = False                   'STOCKID
                .Cols(1).Visible = False                   'ITEMID
                .Cols(2).Visible = False                   'VENDORID
                .Cols(3).Width = .Width * 27 / 100         'ITEMNAME 
                .Cols(4).Width = .Width * 14 / 100         'BATCHNO 
                .Cols(5).Width = .Width * 10 / 100         'EXPDATE
                .Cols(6).Width = .Width * 12 / 100         'REASON
                .Cols(7).Width = .Width * 10 / 100         'QTY
                .Cols(8).Width = .Width * 12 / 100         'BILLRATE
                .Cols(9).Width = .Width * 12 / 100         'AMOUNT
                .Cols(10).Width = .Width * 7 / 100        'RE QTY
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetColumnWidths")
        End Try
    End Sub


    Private Sub btnItems_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnItems.Click
        Dim dtRecord As New DataTable
        Try
            Me.cfgExpItems.Rows.RemoveRange(1, Me.cfgExpItems.Rows.Count - 1)
            If Me.rdnExpItem.Checked = True Then
                '************************************************************
                cfgExpItems.Enabled = True
                strItemType = "Expired"
                dtRecord = BStock.GetExpiredItem("Expired")
                If dtRecord.Rows.Count > 0 Then

                    cfgExpItems.Cols(6).Visible = True                      'Reason
                    cfgExpItems.Cols(6).AllowEditing = False                'Reason
                    cfgExpItems.Cols(7).AllowEditing = False                'Qty
                    'Divya30/01/2008
                    'cfgExpItems.Cols(8).AllowEditing = True              'BILLRATE
                    If rdnNewItem.Checked = True Then
                        cfgExpItems.Cols(8).AllowEditing = False
                    Else
                        cfgExpItems.Cols(8).AllowEditing = True
                    End If
                    '>>END
                    cfgExpItems.Cols(6).Width = cfgExpItems.Width * 14 / 100         'REASON
                    'Populate Expired Record With Grid
                    Call SetGridData(dtRecord)

                    Me.btnSavePR.Visible = True
                    Me.btnSave.Visible = True
                    Me.btnSave.Location = New Point(424, 13)
                    Me.btnSave.Focus()
                Else
                    cfgExpItems.Rows.Count = 17
                    Me.cfgExpItems.Enabled = False
                End If
                'Call Grid_Settings()
                '************************************************************
                '============================================================
            ElseIf Me.rdnNonMove.Checked = True Then
                '************************************************************
                Dim intDays As Integer = 0
                If Me.txtDays.Text <> "" Then
                    intDays = Convert.ToInt32(Me.txtDays.Text.ToString())
                Else
                    cfgExpItems.Rows.Count = 17
                    BCommon.ShowMsgError(10328)
                    Me.txtDays.Focus()
                    cfgExpItems.Enabled = False
                    Exit Sub
                End If
                cfgExpItems.Enabled = True
                dtRecord = BStock.GetNonMoveItem(intDays)
                If dtRecord.Rows.Count > 0 Then
                    cfgExpItems.Cols(6).Visible = True                     'Reason
                    cfgExpItems.Cols(6).AllowEditing = True                'Reason
                    cfgExpItems.Cols(7).AllowEditing = True                'Qty
                    'Divya30/01/2008
                    'cfgExpItems.Cols(8).AllowEditing = True              'BILLRATE
                    If rdnNewItem.Checked = True Then
                        cfgExpItems.Cols(8).AllowEditing = False
                    Else
                        cfgExpItems.Cols(8).AllowEditing = True
                    End If
                    '>>END
                    cfgExpItems.Cols(6).Width = cfgExpItems.Width * 12 / 100         'REASON
                    'Populate Non-Moving Record With Grid
                    Call SetGridData(dtRecord)

                    Me.btnSavePR.Visible = True
                    Me.btnSave.Visible = False
                    Me.btnSave.Location = New Point(530, 13)
                    Me.btnSavePR.Focus()
                Else
                    cfgExpItems.Rows.Count = 17
                    Me.cfgExpItems.Enabled = False
                End If
                'Call Grid_Settings() 
                '************************************************************
                '============================================================
            ElseIf Me.rdnBreakage.Checked = True Then
                '************************************************************
                strItemType = "Breakage"
                cfgExpItems.Enabled = True
                dtRecord = BStock.GetExpiredItem("Breakage")
                If dtRecord.Rows.Count > 0 Then

                    cfgExpItems.Cols(6).Visible = True                      'Enable The Reason Field
                    cfgExpItems.Cols(6).AllowEditing = True                 'Editable The Reason Field
                    cfgExpItems.Cols(7).AllowEditing = True                 'Qty
                    'Divya30/01/2008
                    'cfgExpItems.Cols(8).AllowEditing = True              'BILLRATE
                    If rdnNewItem.Checked = True Then
                        cfgExpItems.Cols(8).AllowEditing = False
                    Else
                        cfgExpItems.Cols(8).AllowEditing = True
                    End If
                    '>>END
                    cfgExpItems.Cols(6).Width = cfgExpItems.Width * 14 / 100         'REASON
                    'Populate Breakage(Damage) and Expired Record With Grid
                    Call SetGridData(dtRecord)

                    Me.btnSave.Visible = False
                    Me.btnSavePR.Visible = True
                    Me.btnSave.Location = New Point(525, 13)
                    Me.btnSavePR.Focus()
                Else
                    cfgExpItems.Rows.Count = 17
                    Me.cfgExpItems.Enabled = False
                End If
                'Me.cfgExpItems.Clear(C1.Win.C1FlexGrid.ClearFlags.All)
                'Call Grid_Settings()
                '************************************************************
                '============================================================
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnItems_Click")
        Finally
            dtRecord = Nothing
        End Try
    End Sub


    Private Sub SetGridData(ByVal dtRecord As DataTable)
        Dim intcount As Integer = 1
        Try
            If dtRecord.Rows.Count > 17 Then
                cfgExpItems.Rows.Count = dtRecord.Rows.Count + 1
            ElseIf dtRecord.Rows.Count = 17 Then
                cfgExpItems.Rows.Count = 18
            Else
                cfgExpItems.Rows.Count = 17
            End If
            'S.STOCKID,S.ITEMID,S.VENDORID,S.BATCHNO,S.EXPDATE,MP.ITEMNAME,S.QUANTITY,S.BILLRATE
            For Each row As DataRow In dtRecord.Rows
                cfgExpItems.Item(intcount, 0) = row.Item("STOCKID")                      'STOCKID
                cfgExpItems.Item(intcount, 1) = row.Item("ITEMID")                       'ITEMID
                cfgExpItems.Item(intcount, 2) = row.Item("VENDORID")                     'VENDORID
                cfgExpItems.Item(intcount, 3) = row.Item("ITEMNAME")                     'ITEMNAME
                cfgExpItems.Item(intcount, 4) = row.Item("BATCHNO")                      'BATCHNO
                cfgExpItems.Item(intcount, 5) = row.Item("EXPDATE")                      'EXPDATE
                If Me.rdnBreakage.Checked = True Then
                    cfgExpItems.Item(intcount, 6) = row.Item("REASON")                   'REASON
                End If
                If Me.rdnExpItem.Checked = True Then
                    cfgExpItems.Item(intcount, 6) = "EXP"                       'REASON
                End If
                cfgExpItems.Item(intcount, 7) = row.Item("QUANTITY")                     'QUANTITY
                cfgExpItems.Item(intcount, 8) = row.Item("BILLRATE")                     'BILLRATE
                cfgExpItems.Item(intcount, 9) = row.Item("QUANTITY") * row.Item("BILLRATE")   'Amount
                'If Me.rdnNonMove.Checked = True Then
                cfgExpItems.Item(intcount, 10) = row.Item("QUANTITY")                   'RETURN QUANTITY
                'End If
                intcount = intcount + 1
            Next
            'Call CalculateAmount()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetGridData")
        End Try
    End Sub

    '******************************************************
    'Validate Date From Current Date
    'Modified By:- Vinit(20071123)
    '*****************************
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim intResult As Integer = 0
        Try
            If ValidateInfo() = True Then
                If Me.rdnNewItem.Checked = True Then
                    strItem = "BR"
                    intResult = SaveData()
                ElseIf Me.rdnExpItem.Checked = True Then
                    strItem = "EX"
                    intResult = SaveData()
                End If
                If intResult > 0 Then
                    BStock.UpdateExpReturnStockDetail(objItem, strItem)
                End If
                If intResult > 0 Then BCommon.ShowMsgError(3) Else BCommon.ShowMsgError(4)

                '*****************************************************************
                'After Save the record,grid row will be delete 
                'And New Breakage Item Entry process will be active.
                Me.cfgExpItems.Rows.RemoveRange(1, Me.cfgExpItems.Rows.Count - 1)
                rdnNewItem.Checked = True
                Call DisplayItems()
                '========================
            End If
            strItem = String.Empty
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnSave_Click")
        End Try
    End Sub
    '******************************************************
    'Validate Date From Current Date
    'Modified By:- Vinit(20071123)
    '*****************************
    Private Sub btnSavePR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSavePR.Click
        Try
            If ValidateInfo() = True Then
                If Me.rdnBreakage.Checked = True Then
                    strItem = "BR"
                    Call SaveNonMoveData()
                    Call UpdateItemQty()
                ElseIf Me.rdnExpItem.Checked = True Then
                    strItem = "EX"
                    Call SaveData()
                    Call SaveNonMoveData()
                    Call UpdateItemStatus()
                ElseIf Me.rdnNonMove.Checked = True Then
                    strItem = "PR"
                    Call SaveNonMoveData()
                End If

                '*****************************************************************************
                'New Field(Status) added in Breakage Table.(20071228)
                'This field will be decide,whether PR complete of this ITEM or not.
                'After Discussion with Micheal.(20071228)
                'If Me.rdnBreakage.Checked = True Then
                '    Call UpdateItemStatus()
                'End If
                'Written By:-Vinit(20071228)
                '===========================                
                'Me.cfgExpItems.Clear(C1.Win.C1FlexGrid.ClearFlags.All)
                Me.cfgExpItems.Rows.RemoveRange(1, Me.cfgExpItems.Rows.Count - 1)
                rdnNewItem.Checked = True
                Call DisplayItems()
            End If
            strItem = String.Empty
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnSave_Click")
        End Try
    End Sub

    '******************************************************
    'Validate All Data 
    'Modified By:- Vinit(20071123)
    '*****************************
    Private Function ValidateInfo() As Boolean


        Try
            If DateTime.Compare(dtpickerDate.Value.date, DateTime.Now()) > 0 Then
                BCommon.ShowMsgError(10228)
                dtpickerDate.Value = DateTime.Now().Date
                dtpickerDate.Focus()
                Return False
            ElseIf cfgExpItems.Rows.Count <= 1 Then
                BCommon.ShowMsgError(10265)
                Return False
            ElseIf Me.rdnNonMove.Checked = True And Me.txtDays.Text = "" Then
                BCommon.ShowMsgError(10328)
                Me.txtDays.Focus()
                Return False
            ElseIf IsNothing(cfgExpItems.Item(1, 3)) = True Then
                BCommon.ShowMsgError(10230)
                cfgExpItems.Focus()
                cfgExpItems.Col = 3
                cmDItems.Focus()
                Return False
            ElseIf IsNothing(cfgExpItems.Item(1, 4)) = True Then
                BCommon.ShowMsgError(10236)
                cfgExpItems.Focus()
                cfgExpItems.Col = 4
                CmbBatchNo.Focus()
                Return False
                ''ElseIf cfgExpItems.Rows.Count - 2 >= 0 Then
                ''    For intcount = 0 To cfgExpItems.Rows.Count - 2
                ''        If cfgExpItems.Item(intcount + 1, 8) > 0.0 And cfgExpItems.Item(intcount + 1, 8) <> Nothing Then
                ''            intFlag = 1
                ''        End If
                ''    Next
                ''If intFlag = 1 Then
                ''    Return True
                ''Else
                ''    BCommon.ShowMsgError(10116)
                ''    cfgExpItems.Focus()
                ''    cfgExpItems.Col = 8
                ''    Return False
                ''End If
            Else
                Return True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnSave_Click")
        End Try
    End Function

    '******************************************************
    'Save Data In Breakage Table 
    'Modified By:- Vinit(20071123)
    '*****************************
    Private Function SaveData() As Integer
        Dim intResult As Integer
        Dim intItems As Integer
        Dim intCount As Integer
        Dim intObjCount As Integer = 0
        Try
            With cfgExpItems
                For intCount = 1 To .Rows.Count - 1
                    If .Item(intCount, 0) <> Nothing OrElse Trim(.Item(intCount, 0)) <> "" Then intItems = intItems + 1
                Next

                ReDim objItem(intItems - 1)
                'For intCount = 0 To intItems - 1
                '    objItem(intCount) = New CExpItem
                '    objItem(intCount).StockID = .Item(intCount + 1, 0)
                '    objItem(intCount).ItemID = .Item(intCount + 1, 1)
                '    objItem(intCount).VendorID = .Item(intCount + 1, 2)
                '    objItem(intCount).BatchNo = .Item(intCount + 1, 4)
                '    objItem(intCount).ExpiryDate = .Item(intCount + 1, 5)
                '    objItem(intCount).ItemName = .Item(intCount + 1, 3)
                '    objItem(intCount).Reason = .Item(intCount + 1, 6)
                '    objItem(intCount).Quantity = .Item(intCount + 1, 7)
                '    objItem(intCount).BillRate = .Item(intCount + 1, 8)
                '    objItem(intCount).BreakageDate = dtpickerDate.Value.date
                '    objItem(intCount).Status = "A"
                'Next
                For intCount = 0 To intItems - 1
                    'Check the quantity is greater than zero or not.
                    If .Item(intCount + 1, 7) > 0 Then
                        objItem(intObjCount) = New CExpItem
                        objItem(intObjCount).StockID = .Item(intCount + 1, 0)
                        objItem(intObjCount).ItemID = .Item(intCount + 1, 1)
                        objItem(intObjCount).VendorID = .Item(intCount + 1, 2)
                        objItem(intObjCount).BatchNo = .Item(intCount + 1, 4)
                        objItem(intObjCount).ExpiryDate = .Item(intCount + 1, 5)
                        objItem(intObjCount).ItemName = .Item(intCount + 1, 3)
                        objItem(intObjCount).Reason = .Item(intCount + 1, 6)
                        objItem(intObjCount).Quantity = .Item(intCount + 1, 7)
                        objItem(intObjCount).BillRate = .Item(intCount + 1, 8)
                        objItem(intObjCount).BreakageDate = dtpickerDate.Value.date.Date
                        objItem(intObjCount).Status = "A"
                        intObjCount = intObjCount + 1
                    End If
                Next
            End With

            intResult = BStock.SaveItems(objItem)
            Return intResult
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SaveData")
        End Try
    End Function


    '******************************************************
    'Save Data In for the Purchase Return
    'Modified By:- Vinit(20071123)
    '*****************************
    Private Sub SaveNonMoveData()
        Dim intResult As Integer
        Dim intItems As Integer
        Dim intCount As Integer
        Dim intItemCount As Integer = 0
        Dim intFlag As Integer = 0
        'Dim intObjCount As Integer = 0
        Try
            With cfgExpItems
                For intCount = 1 To .Rows.Count - 1
                    If .Item(intCount, 0) <> Nothing OrElse Trim(.Item(intCount, 0)) <> "" Then intItems = intItems + 1
                Next
                ReDim ObjRetItems(intItems - 1)

                CPurInv = New CPurchaseInvoice
                CPurInv.VendorID = cfgExpItems.Item(1, 2)
                CPurInv.BillDate = dtpickerDate.Value.Date
                CPurInv.Remarks = ""

                For intCount = 0 To intItems - 1
                    If CPurInv.VendorID = .Item(intCount + 1, 2) Then
                        If .Item(intCount + 1, 7) > 0 Then
                            ObjRetItems(intItemCount) = New CReturnItem
                            ObjRetItems(intItemCount).ItemID = .Item(intCount + 1, 1)
                            ObjRetItems(intItemCount).BatchNo = .Item(intCount + 1, 4)
                            ObjRetItems(intItemCount).Quantity = .Item(intCount + 1, 7)
                            ObjRetItems(intItemCount).Rate = .Item(intCount + 1, 8)
                            ObjRetItems(intItemCount).Amount = .Item(intCount + 1, 9)
                            ObjRetItems(intItemCount).Reason = .Item(intCount + 1, 6)
                            intItemCount = intItemCount + 1
                        End If
                    Else
                        intFlag = 1
                        If IsNothing(ObjRetItems(0)) <> True Then
                            intResult = BPurchaseInvoice.SaveExpPurReturn("S", CPurInv, ObjRetItems, strItem)
                        End If

                        CPurInv.VendorID = cfgExpItems.Item(intCount + 1, 2)
                        CPurInv.BillDate = dtpickerDate.Value.Date
                        CPurInv.Remarks = ""
                        '***************************************************
                        'Clear the previous record from the Object
                        Array.Clear(ObjRetItems, 0, intItemCount)
                        intItemCount = 0
                        '========================================
                        If .Item(intCount + 1, 7) > 0 Then
                            ObjRetItems(intItemCount) = New CReturnItem
                            ObjRetItems(intItemCount).ItemID = .Item(intCount + 1, 1)
                            ObjRetItems(intItemCount).BatchNo = .Item(intCount + 1, 4)
                            ObjRetItems(intItemCount).Quantity = .Item(intCount + 1, 7)
                            ObjRetItems(intItemCount).Rate = .Item(intCount + 1, 8)
                            ObjRetItems(intItemCount).Amount = .Item(intCount + 1, 9)
                            ObjRetItems(intItemCount).Reason = .Item(intCount + 1, 6)
                            intItemCount = intItemCount + 1
                        End If
                    End If
                Next
                'If intFlag = 0 Then
                If IsNothing(ObjRetItems(0)) <> True Then
                    intResult = BPurchaseInvoice.SaveExpPurReturn("S", CPurInv, ObjRetItems, strItem)
                End If
                'End If
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SaveNonMoveData")
        End Try
        'intResult = BPurchaseInvoice.SaveRetun("S", CPurInv, ObjRetItems)
        If intResult > 0 Then BCommon.ShowMsgError(3) Else BCommon.ShowMsgError(4)
    End Sub


    Private Sub DisplayItems()
        Dim dtDItems As New DataTable
        Try
            Me.cfgExpItems.Rows.Count = 17  'Modified By:-VINIT(2008/01/07)
            If rdnNewItem.Checked = True Then
                dtDItems = BPurchaseInvoice.GetReturnItems(0, True)
            End If
            If Not IsNothing(dtDItems) AndAlso dtDItems.Rows.Count > 0 Then
                'Modified By:-VINIT(2008/02/10)
                If dtDItems.Rows.Count > 17 Then
                    cfgExpItems.Rows.Count = dtDItems.Rows.Count + 1
                ElseIf dtDItems.Rows.Count = 17 Then
                    cfgExpItems.Rows.Count = 18
                Else
                    cfgExpItems.Rows.Count = 17
                End If
                cmDItems.DataSource = dtDItems
                cmDItems.ValueMember = "ItemID"
                cmDItems.DisplayMember = "ItemName"
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "DisplayItems")
        Finally
            dtDItems = Nothing
        End Try
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub


    Private Sub cmDItems_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmDItems.KeyDown
        If e.KeyCode = Keys.Enter Then
            cfgExpItems.Col = 4
        End If
    End Sub

    Private Sub cfgExpItems_AfterEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles cfgExpItems.AfterEdit
        Try
            With cfgExpItems
                Select Case .Col
                    Case 4          'Batch
                        Call SetItemRow()
                    Case 7
                        If IsNothing(.Item(.Row, 7)) Or CDbl(.Item(.Row, 7)) < 0 Then
                            BCommon.ShowMsgError(10225)
                            .Col = 7
                            .Item(.Row, 7) = .Item(.Row, 10)
                            .Item(.Row, 9) = .Item(.Row, 7) * .Item(.Row, 8)
                            Exit Sub
                        Else
                            If CDbl(.Item(.Row, 7)) < 0.0 Then
                                BCommon.ShowMsgError(10114)
                                .Col = 7
                                .Item(.Row, 7) = .Item(.Row, 10)
                                .Item(.Row, 9) = .Item(.Row, 7) * .Item(.Row, 8)
                                Exit Sub
                            ElseIf CDbl(.Item(.Row, 7)) > CDbl(.Item(.Row, 10)) Then
                                BCommon.ShowMsgError(10143)
                                .Col = 7
                                .Item(.Row, 7) = .Item(.Row, 10)
                                .Item(.Row, 9) = .Item(.Row, 7) * .Item(.Row, 8)
                                Exit Sub
                            End If
                        End If
                        .Item(.Row, 9) = .Item(.Row, 7) * .Item(.Row, 8)
                    Case 8
                        If IsNothing(.Item(.Row, 8)) Or CDbl(.Item(.Row, 8)) <= 0 Then
                            BCommon.ShowMsgError(10116)
                            .Col = 8
                            .Item(.Row, 7) = .Item(.Row, 10)
                            .Item(.Row, 9) = .Item(.Row, 7) * .Item(.Row, 8)
                            Exit Sub
                        Else
                            If CDbl(.Item(.Row, 8)) > 0 Then
                                .Item(.Row, 9) = .Item(.Row, 7) * .Item(.Row, 8)
                                Exit Sub
                            End If
                        End If
                        .Item(.Row, 9) = .Item(.Row, 7) * .Item(.Row, 8)
                End Select
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cfgRetItems_AfterEdit")
        End Try
    End Sub

    Private Sub cfgExpItems_BeforeEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles cfgExpItems.BeforeEdit
        Try
            If e.Col = 4 Then
                dtDItemDtl = BPurchaseInvoice.GetReturnItemDtl(0, cmDItems.SelectedValue, True)
                If Not IsNothing(cfgExpItems.Item(e.Row, 3)) Then
                    If Not IsNothing(dtDItemDtl) Then
                        If dtDItemDtl.Rows.Count > 0 Then
                            CmbBatchNo.DataSource = dtDItemDtl
                            CmbBatchNo.ValueMember = "StockID"
                            CmbBatchNo.DisplayMember = "BatchNo"
                        End If
                    End If
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cfgExpItems_BeforeEdit")
        End Try
    End Sub

    Private Sub rdnBrakage_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdnBreakage.CheckedChanged
        Try
            If Me.rdnBreakage.Checked = True Then
                Call Grid_Settings()

                Me.cmDItems.DataSource = Nothing
                Me.CmbBatchNo.DataSource = Nothing
                Me.cmDItems.Visible = False
                Me.CmbBatchNo.Visible = False
                cfgExpItems.Cols(3).Editor = Nothing
                cfgExpItems.Cols(3).AllowEditing = False                'ITEMNAME
                cfgExpItems.Cols(4).Editor = Nothing
                cfgExpItems.Cols(4).AllowEditing = False                'BATCHNO

                rdnBreakage.Focus()
                cfgExpItems.Enabled = False

                strItemType = "Breakage"
                Me.txtDays.Text = ""
                Me.txtDays.Enabled = False
                Me.btnItems.Enabled = True
                Me.btnSave.Visible = False
                Me.btnSavePR.Visible = False
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "rdnBrakage_CheckedChanged")
        End Try
    End Sub

    Private Sub CmbBatchNo_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles CmbBatchNo.KeyDown
        'If e.KeyCode = Keys.Enter Then
        '    cfgExpItems.Col = 5
        'End If
    End Sub

    Private Sub rdnExpItem_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdnExpItem.CheckedChanged
        Try
            If Me.rdnExpItem.Checked = True Then
                Call Grid_Settings()
                Me.cmDItems.DataSource = Nothing
                Me.CmbBatchNo.DataSource = Nothing
                Me.cmDItems.Visible = False
                Me.CmbBatchNo.Visible = False
                cfgExpItems.Cols(3).Editor = Nothing
                cfgExpItems.Cols(3).AllowEditing = False                'ITEMNAME
                cfgExpItems.Cols(4).Editor = Nothing
                cfgExpItems.Cols(4).AllowEditing = False                'BATCHNO
                rdnExpItem.Focus()
                Me.cfgExpItems.Enabled = False

                strItemType = "Expired"
                Me.txtDays.Text = ""
                Me.txtDays.Enabled = False
                Me.btnItems.Enabled = True
                Me.btnSave.Visible = False
                Me.btnSavePR.Visible = False
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "rdnExpItem_CheckedChanged")
        End Try
    End Sub

    Private Sub rdnNonMove_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdnNonMove.CheckedChanged
        Try
            If Me.rdnNonMove.Checked = True Then
                Call Grid_Settings()

                Me.cmDItems.DataSource = Nothing
                Me.CmbBatchNo.DataSource = Nothing
                Me.cmDItems.Visible = False
                Me.CmbBatchNo.Visible = False
                cfgExpItems.Cols(3).Editor = Nothing
                cfgExpItems.Cols(3).AllowEditing = False               'ITEMNAME
                cfgExpItems.Cols(4).Editor = Nothing
                cfgExpItems.Cols(4).AllowEditing = False                'BATCHNO

                rdnNonMove.Focus()
                cfgExpItems.Enabled = False

                Me.txtDays.Text = ""
                Me.txtDays.Enabled = True
                Me.btnItems.Enabled = True
                Me.btnSave.Visible = False
                Me.btnSavePR.Visible = False
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "rdnNonMove_CheckedChanged")
        End Try
    End Sub


    Private Sub SetItemRow()
        'Sets the default values for selected row
        Dim intCount As Integer
        Try
            Dim intItemID As Integer, StockID As Integer
            With cfgExpItems
                Select Case .Col
                    Case 4
                        If cmDItems.SelectedValue > 0 And CmbBatchNo.SelectedValue > 0 Then
                            intItemID = cmDItems.SelectedValue
                            StockID = dtDItemDtl.Rows(CmbBatchNo.SelectedIndex).Item(3)
                        End If
                End Select
                If .Row > 0 Then
                    For intCount = 1 To .Rows.Count - 1
                        .Rows(.Row).Clear(C1.Win.C1FlexGrid.ClearFlags.Content)
                        If StockID <> 0 Then
                            If .Item(intCount, 1) = intItemID And .Item(intCount, 0) = StockID Then
                                BCommon.ShowMsgError(10268)
                                '.Rows.Remove(.Row)  Modified By:-Vinit(20071105)
                                .Col = 2
                                Exit Sub
                            End If
                        End If
                    Next
                End If
                dtDItemDtl.PrimaryKey = New DataColumn() {dtDItemDtl.Columns(3)} 'Batch ID

                Dim dtrItem As DataRow = dtDItemDtl.Rows.Find((StockID)) '(Items)
                If IsNothing(dtrItem) Then Exit Sub
                .Item(.Row, 0) = dtrItem("StockID")                                'STOCKID
                .Item(.Row, 1) = dtrItem("ItemID")                                'ITEM ID
                .Item(.Row, 3) = dtrItem("ItemName")                                'ITEM NAME
                .Item(.Row, 4) = dtrItem("BatchNo")                                'BATCH
                .Item(.Row, 5) = dtrItem("EXPDATE")                              'EXPIRY

                .Item(.Row, 7) = dtrItem("Quantity")                                'QUANTITY
                .Item(.Row, 8) = dtrItem("BillRate")                                'RATE
                .Item(.Row, 9) = dtrItem("Quantity") * dtrItem("BillRate")          'AMOUNT
                .Item(.Row, 10) = dtrItem("Quantity")                                'Return QUANTITY
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetItemRow")
        End Try
    End Sub

    Private Sub cfgExpItems_BeforeRowColChange(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RangeEventArgs) Handles cfgExpItems.BeforeRowColChange
        If e.NewRange.r1 <> e.OldRange.r1 Then
            Me.CmbBatchNo.DataSource = Nothing
        End If
    End Sub

    Private Sub cfgExpItems_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cfgExpItems.KeyDown
        If e.KeyCode = Keys.Delete Then
            cfgExpItems.Rows.Remove(cfgExpItems.Row)
            cfgExpItems.Rows.Count = cfgExpItems.Rows.Count + 1
        End If
    End Sub

    Private Sub cfgExpItems_SetupEditor(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles cfgExpItems.SetupEditor

        With cfgExpItems
            Select Case .Col
                Case 6  'REASON
                    Dim txt As TextBox = DirectCast(cfgExpItems.Editor, TextBox)
                    txt.MaxLength = 100
                Case 7  'QTY
                    Dim txt As TextBox = DirectCast(cfgExpItems.Editor, TextBox)
                    txt.MaxLength = 6
                Case 8, 9, 10 'BillRate 'AMOUNT  'RETURN QTY
                    Dim txt As TextBox = DirectCast(cfgExpItems.Editor, TextBox)
                    txt.MaxLength = 13
                    'Case 9 'AMOUNT
                    '    CType(ctl, TextBox).MaxLength = 13
                    'Case 10 'RETURN QTY
                    '    CType(ctl, TextBox).MaxLength = 13
            End Select
        End With
    End Sub

    Private Sub cfgExpItems_ValidateEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.ValidateEditEventArgs) Handles cfgExpItems.ValidateEdit
        Try
            'If e.Col = 8 Then
            '    If cfgExpItems.Editor.Text <> "" Then
            '        If IsNumeric(cfgExpItems.Editor.Text.ToString()) = False Then
            '            BCommon.ShowMsgError(10114)
            '            cfgExpItems.Editor.Text = cfgExpItems.Item(e.Row, e.Col + 3)
            '            e.Cancel = True
            '        ElseIf Convert.ToDouble(cfgExpItems.Editor.Text.ToString()) > cfgExpItems.Item(e.Row, e.Col + 3) Then
            '            BCommon.ShowMsgError(10266)
            '            cfgExpItems.Editor.Text = cfgExpItems.Item(e.Row, e.Col + 3)
            '            e.Cancel = True
            '        ElseIf Convert.ToDouble(cfgExpItems.Editor.Text.ToString()) < 0 Then
            '            BCommon.ShowMsgError(10114)
            '            cfgExpItems.Editor.Text = cfgExpItems.Item(e.Row, e.Col + 3)
            '            e.Cancel = True
            '        End If
            '    End If
            'End If
            If e.Col = 7 Then
                If cfgExpItems.Editor.Text <> "" Then
                    If IsNumeric(cfgExpItems.Editor.Text.ToString()) = False Then
                        BCommon.ShowMsgError(10114)
                        cfgExpItems.Editor.Text = cfgExpItems.Item(e.Row, e.Col + 3)
                        e.Cancel = True
                    ElseIf Convert.ToDouble(cfgExpItems.Editor.Text.ToString()) > cfgExpItems.Item(e.Row, e.Col + 3) Then
                        BCommon.ShowMsgError(10266)
                        cfgExpItems.Editor.Text = cfgExpItems.Item(e.Row, e.Col + 3)
                        e.Cancel = True
                    ElseIf Convert.ToDouble(cfgExpItems.Editor.Text.ToString()) < 0 Then
                        BCommon.ShowMsgError(10114)
                        cfgExpItems.Editor.Text = cfgExpItems.Item(e.Row, e.Col + 3)
                        e.Cancel = True
                    End If
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "FGCheqDetails_KeyDown")
        End Try
    End Sub


    Private Function isUpdate() As Boolean
        Try
            If cfgExpItems.Rows.Count > 1 Then
                If IsNothing(cfgExpItems.Item(1, 3)) Or cfgExpItems.Item(1, 3) = String.Empty Then
                    Return False
                Else
                    Return True
                End If
            Else
                Return False
            End If
            Return True
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "isUpdate")
        End Try
    End Function

    '*********************************************************************************
    'Changing the Status of the Record in Breakage Table After Completion of PR
    'In Case of Expiry Item
    'Modified By:- Vinit(20071123)
    '*****************************
    Private Sub UpdateItemStatus()
        Try
            Dim intCount As Integer = 0
            Dim intItems As Integer = 0
            With cfgExpItems
                For intCount = 1 To .Rows.Count - 1
                    If .Item(intCount, 0) <> Nothing OrElse Trim(.Item(intCount, 0)) <> "" Then intItems = intItems + 1
                Next
                ReDim objItem(intItems - 1)
                For intCount = 0 To intItems - 1
                    objItem(intCount) = New CExpItem
                    objItem(intCount).StockID = .Item(intCount + 1, 0)
                    objItem(intCount).Status = "PR"
                Next
            End With
            BStock.UpdateBrkItemsStatus(objItem)
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "UpdateItemStatus")
        End Try
    End Sub


    '*********************************************************************************
    'Update the Quantity of the Record in Breakage Table After Completion of PR
    'In Case of Damage and Expiry Item(Breakage Item)
    'Modified By:- Vinit(20071123)
    '*****************************
    Private Sub UpdateItemQty()
        Try
            Dim intCount As Integer = 0
            Dim intItems As Integer = 0
            With cfgExpItems
                For intCount = 1 To .Rows.Count - 1
                    If .Item(intCount, 0) <> Nothing OrElse Trim(.Item(intCount, 0)) <> "" Then intItems = intItems + 1
                Next
                ReDim objItem(intItems - 1)
                For intCount = 0 To intItems - 1
                    objItem(intCount) = New CExpItem
                    objItem(intCount).StockID = .Item(intCount + 1, 0)
                    objItem(intCount).Quantity = .Item(intCount + 1, 7)
                Next
            End With
            BStock.UpdateBrkItemsQty(objItem)
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "UpdateItemStatus")
        End Try
    End Sub


    '*****************************************************************************
    'Written By:-VINIT(2008/01/07) For the New Breakage Item Insertion
    '=============================
    Private Sub rdnNewItem_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdnNewItem.CheckedChanged
        Try
            If Me.rdnNewItem.Checked = True Then
                Me.cfgExpItems.Enabled = True
                Call Grid_Settings()
                Call DisplayItems()

                cfgExpItems.Cols(3).Editor = cmDItems
                cfgExpItems.Cols(3).AllowEditing = True              'ITEMNAME

                cfgExpItems.Cols(4).Editor = CmbBatchNo
                cfgExpItems.Cols(4).AllowEditing = True              'BATCHNO

                'cfgExpItems.Cols(6).Visible = True                   'Reason
                cfgExpItems.Cols(6).AllowEditing = True              'Reason

                cfgExpItems.Cols(7).AllowEditing = True              'Qty
                'Divya30/01/2008
                'cfgExpItems.Cols(8).AllowEditing = True              'BILLRATE
                If rdnNewItem.Checked = True Then
                    cfgExpItems.Cols(8).AllowEditing = False
                Else
                    cfgExpItems.Cols(8).AllowEditing = True
                End If
                '>>END

                Me.txtDays.Enabled = False
                Me.btnItems.Enabled = False
                Me.btnSavePR.Visible = False
                Me.btnSave.Visible = True
                Me.btnSave.Location = New Point(542, 13)
                Me.txtDays.Text = ""

                '**************************************
                'Setting the Tab to Grid First Item
                'cfgExpItems.Focus()
                'cfgExpItems.Row = 1
                cfgExpItems.Col = 3
                Me.cmDItems.Focus()
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "rdnBrakage_CheckedChanged")
        End Try
    End Sub

    Private Sub txtDays_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDays.KeyDown
        Try
            If e.Alt Then Exit Sub
            If e.KeyCode = Keys.Enter Then Me.btnItems.Focus()
            If (BCommon.IsInteger(txtDays.Text) = False) Then txtDays.Focus() : txtDays.Text = ""
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtfax_KeyUp")
        End Try
    End Sub

    Private Sub txtDays_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDays.LostFocus
        'Try
        '    If txtDays.Text = "" Then BCommon.ShowMsgError(10328) : txtDays.Focus() : txtDays.Text = "" : Exit Sub

        '    If (BCommon.IsInteger(txtDays.Text) = False) Then txtDays.Focus() : txtDays.Text = ""
        'Catch ex As Exception
        '    BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtfax_KeyUp")
        'End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.rdnNewItem.Checked = True
        Me.cfgExpItems.Rows.RemoveRange(1, Me.cfgExpItems.Rows.Count - 1)
        Me.cfgExpItems.Rows.Count = 17
        Call DisplayItems()
        Me.txtDays.Text = ""
        Me.cfgExpItems.Col = 3
        Me.cmDItems.Focus()
    End Sub
End Class